package com.micro.ai.agents.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.agents.entity.Message;
import com.micro.ai.agents.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 消息控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/messages")
@Tag(name = "消息管理", description = "消息的创建、查询等功能")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @PostMapping
    @Operation(summary = "创建消息", description = "创建新的消息")
    public ApiResponse<Message> create(@Valid @RequestBody Message message) {
        Message result = messageService.create(message);
        return ApiResponse.success("创建成功", result);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建消息", description = "批量创建消息")
    public ApiResponse<Void> batchCreate(@Valid @RequestBody List<Message> messages) {
        messageService.batchCreate(messages);
        return ApiResponse.successVoid("批量创建成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取消息详情", description = "根据ID获取消息详细信息")
    public ApiResponse<Message> getById(
            @Parameter(description = "消息ID", required = true) @PathVariable String id) {
        Message message = messageService.getById(id);
        if (message == null) {
            return ApiResponse.error("A0002", "消息不存在");
        }
        return ApiResponse.success(message);
    }

    @GetMapping("/by-conversation/{conversationId}")
    @Operation(summary = "查询会话消息", description = "根据会话ID查询消息列表")
    public ApiResponse<List<Message>> listByConversationId(
            @Parameter(description = "会话ID", required = true) @PathVariable String conversationId) {
        List<Message> list = messageService.listByConversationId(conversationId);
        return ApiResponse.success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询消息", description = "支持多条件查询消息列表")
    public ApiResponse<Page<Message>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "会话ID") @RequestParam(value = "conversationId", required = false) String conversationId,
            @Parameter(description = "角色") @RequestParam(value = "role", required = false) String role) {
        Page<Message> page = messageService.pageQuery(pageNum, pageSize, tenantId, conversationId, role);
        return ApiResponse.success(page);
    }
}

